package com.kupurui.asteacher.ui.job;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.SelGradeAdapter;
import com.kupurui.asteacher.adapter.SelSubjectAdapter;
import com.kupurui.asteacher.bean.GradeInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 选择年纪及科目
 * Created by Administrator on 2017/7/24.
 */

public class SelGradeSubjectAty extends BaseAty {

    @Bind(R.id.tv_bar_title)
    TextView tvBarTitle;
    @Bind(R.id.m_toolbar)
    Toolbar mToolbar;
    @Bind(R.id.listview_grade)
    ListView listviewGrade;
    @Bind(R.id.listview_subject)
    ListView listviewSubject;


    GradeInfo gradeInfo;
    List<GradeInfo.GradeBean> gradeList;
    SelGradeAdapter gradeAdapter;
    List<GradeInfo.GradeBean.SubjectBean> subjectList;
    SelSubjectAdapter subjectAdapter;

    private String grade = "";
    private String grade_id = "";
    @Override
    public int getLayoutId() {
        return R.layout.sel_grade_subject_aty;
    }

    @Override
    public void initData() {
        initToolbar(mToolbar,"选择年级及科目");
        gradeList = new ArrayList<>();
        gradeAdapter = new SelGradeAdapter(this,gradeList,R.layout.textview_layout);
        gradeAdapter.setChooseIndex(0);
        listviewGrade.setAdapter(gradeAdapter);
        listviewGrade.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gradeAdapter.setChooseIndex(position);
                gradeAdapter.notifyDataSetChanged();
                subjectList.clear();
                subjectList.addAll(gradeList.get(position).getSubject());
                subjectAdapter.notifyDataSetChanged();
                grade = gradeList.get(position).getTitle();
                grade_id = gradeList.get(position).getId();
            }
        });
        subjectList = new ArrayList<>();
        subjectAdapter = new SelSubjectAdapter(this,subjectList,R.layout.div_textview_layout);
        listviewSubject.setAdapter(subjectAdapter);
        listviewSubject.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                subjectAdapter.setChooseIndex(position);
                subjectAdapter.notifyDataSetChanged();
                Bundle bundle = new Bundle();
                bundle.putString("grade", grade);
                bundle.putString("subject", subjectList.get(position).getSubject());
                bundle.putString("grade_id",grade_id);
                Intent intent = new Intent();
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().chooseGrade(this,0);
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                gradeInfo = AppJsonUtil.getObject(result,GradeInfo.class);
                gradeList.clear();
                gradeList.addAll(gradeInfo.getGrade());
                gradeAdapter.notifyDataSetChanged();
                subjectList.clear();
                subjectList.addAll(gradeList.get(0).getSubject());
                grade = gradeList.get(0).getTitle();
                grade_id = gradeList.get(0).getId();
                subjectAdapter.notifyDataSetChanged();
                break;
        }
        super.onSuccess(result, call, response, what);
    }
}
